package com.yupi.oj.judge.codesanbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yupi.oj.exception.BusinessException;
import com.yupi.oj.judge.codesanbox.CodeSanBox;
import com.yupi.oj.judge.codesanbox.model.ExecuteCodeRequest;
import com.yupi.oj.judge.codesanbox.model.ExecuteCodeResponse;

public class RemoteCodeSanBox implements CodeSanBox {
    private static final String AUTH_REQUEST_HEADER = "Auth";
    private static final String AUTH_REQUEST_SECRET = "123456";

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        String url = "http://localhost:8082/executeCode";
        String json = JSONUtil.toJsonStr(executeCodeRequest);
        String body = HttpUtil.createPost(url).header(AUTH_REQUEST_HEADER,AUTH_REQUEST_SECRET).body(json).execute().body();
        if(body == null){
            throw new BusinessException(50002,"调用CodeSangBox失败");
        }

        return JSONUtil.toBean(body, ExecuteCodeResponse.class);
    }
}



